/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.image;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import javax.swing.JComponent;
import javax.swing.TransferHandler;

/**
 *
 * @author HY
 */
public class LoadedImageImportTransferHandler
        extends TransferHandler
{
    @Override
    public boolean canImport( TransferSupport support )
    {
        System.out.println( "Import request from " + support.getComponent().getClass() );
        boolean result = false;
        DataFlavor[] flavors = support.getDataFlavors();
        if ( flavors != null )
        {
            for( int i = 0; i < flavors.length; i++ )
            {
                if( LoadedImage.MIME_TYPE.equals( flavors[ i ].getMimeType() ) )
                {
                    result = true;
                }
            }
        }
        else
        {
            result = super.canImport( support );
        }
        System.out.println( "Import request " + ( ( result )? "approved" : "rejected" ) );
        return result;
    }

    @Override
    public boolean canImport( JComponent comp, DataFlavor[] transferFlavors )
    {
        boolean result = false;
        if( comp instanceof ImageSlide )
        {
            if( transferFlavors != null )
            {
                for( int i = 0; i < transferFlavors.length; i++ )
                {
                    if( LoadedImage.MIME_TYPE.equals( transferFlavors[ i ].getMimeType() ) )
                    {
                        result = true;
                    }
                }
            }
        }
        else
        {
            return super.canImport( comp, transferFlavors );
        }
        return result;
    }

    @Override
    protected Transferable createTransferable( JComponent c )
    {
        if( c instanceof ImageSlide )
        {
            return ( (ImageSlide) c ).getImage();
        }
        else
        {
            return super.createTransferable( c );
        }
    }

    @Override
    public boolean importData( TransferSupport support )
    {
        System.out.println( "Import request from " + support.getComponent().getClass() );
        boolean result = false;
        if( support.getComponent() instanceof ImagePlaceComponent )
        {
            ImagePlaceComponent imageFrame = (ImagePlaceComponent) support.getComponent();
            Transferable transferData = support.getTransferable();
            if( transferData instanceof LoadedImage )
            {
                imageFrame.setImage( (LoadedImage) transferData );
                result = true;
            }
        }
        else if( support.getComponent() instanceof ImageFrameComponent )
        {
            ImageFrameComponent imageFrame = (ImageFrameComponent) support.getComponent();
            Transferable transferData = support.getTransferable();
            if( transferData instanceof LoadedImage )
            {
                imageFrame.getImageFrame().setLoadedImage( (LoadedImage) transferData );
                result = true;
            }
        }
        else
        {
            result = super.importData( support );
        }
        System.out.println( "Import request " + ( ( result )? "approved" : "rejected" ) );
        return result;
    }
}
